package owlapps2012.base;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;

import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.OWLDataFactory;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.semanticweb.owlapi.model.OWLOntologyManager;
import org.semanticweb.owlapi.model.OWLOntologyStorageException;

public abstract class FactsImport
{

	protected OWLOntology owlToPrepare;
	protected OWLOntologyManager manager;
	protected OWLDataFactory factory;
	protected ArrayList<String> listOfIDs;
	
	public FactsImport()
	{
		super();
		this.init();
	}
	
	public FactsImport(String sqlToExtractFactsIDs) throws SQLException
	{
		this();
		this.listOfIDs = this.extractFactsIDsFromDatabase(sqlToExtractFactsIDs);
	}
	
	public FactsImport(String[] objectIDs)
	{
		this();
		this.listOfIDs = new ArrayList<String>(Arrays.asList(objectIDs));
	}
	
	protected void init() 
	{
		manager = OWLManager.createOWLOntologyManager();
		factory = manager.getOWLDataFactory();
	}
	
	public OWLOntology getOwlToPrepare()
	{
		return owlToPrepare;
	}
	
	public OWLOntology prepareOWL() throws Exception 
	{
		if(!listOfIDs.isEmpty())
		{
			importFacts(listOfIDs.get(0));
			listOfIDs.remove(0);
			return owlToPrepare;
		}
		else
			return null;
	}


	protected abstract void initOWLToPrepare(String owlFileName) throws OWLOntologyCreationException, OWLOntologyStorageException;

	protected abstract ArrayList<String> extractFactsIDsFromDatabase(String sqlToExtractFactsIDs) throws SQLException;
	
	protected abstract void importFacts(String factID) throws Exception;// mapping data to owlToPrepare

}
